package com.dinzeer.legendreliclib.effect;

import com.dinzeer.legendreliclib.lib.util.FastMakeEntityUtil;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod.EventBusSubscriber
public class TheGreatRip extends MobEffect {

    public TheGreatRip(MobEffectCategory category, int color) {
        super(category, color);
    }

    // 每秒执行一次伤害
    @Override
    public void applyEffectTick(LivingEntity entity, int amplifier) {

            // 计算伤害 = 当前生命值 * (10% * (等级+1))
            float damagePercentage = 0.10F * (amplifier + 1);
            float damageAmount = entity.getHealth() * damagePercentage;

            // 造成可被护甲减免的伤害
            entity.hurt(entity.damageSources().magic(), damageAmount);

    }

    // 每20游戏刻（1秒）触发一次
    @Override
    public boolean isDurationEffectTick(int duration, int amplifier) {
        return duration % 20 == 0; // 每秒触发
    }


        @SubscribeEvent
        public static void onEntityHurt(LivingHurtEvent event) {
            LivingEntity victim = event.getEntity();

            // 检查受害者是否有撕裂效果
            victim.getActiveEffects().stream()
                    .filter(effect -> effect.getEffect() instanceof TheGreatRip)
                    .findFirst()
                    .ifPresent(effect -> {
                        // 获取攻击者
                        LivingEntity attacker = null;
                        if (event.getSource().getEntity() instanceof LivingEntity) {
                            attacker = (LivingEntity) event.getSource().getEntity();
                        }

                        // 恢复攻击者生命值（等级 = amplifier+1）
                        if (attacker != null) {
                            int healAmount = effect.getAmplifier() + 1;
                            attacker.heal(healAmount);
                        }
                    });
        }

}